package com.edfu.core.coursemgt.service.ws;

import java.util.List;

import javax.jws.WebService;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;

import com.edfu.core.coursemgt.service.api.ICourseOfferingMgtService;
import com.edfu.core.coursemgt.vo.CourseOffering;
import com.edfu.core.coursemgt.vo.Lesson;
import com.edfu.core.coursemgt.vo.LessonUnit;

@Path("/")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_JSON)
@WebService(targetNamespace = "http://com.edfu.core.coursemgt.service.ws.CourseOfferingMgtService")
public class CourseOfferingMgtServiceDelegate {

	@Autowired
	ICourseOfferingMgtService courseOfferingMgtService;

	@POST
	@Path("/courseofferings")
	public Response addCourseOffering(CourseOffering courseOffering) {
		CourseOffering co = courseOfferingMgtService
				.addCourseOffering(courseOffering);
		return Response.ok(co).build();
	}

	@PUT
	@Path("/courseofferings")
	public Response updateCourseOffering(CourseOffering courseOffering) {
		courseOfferingMgtService.updateCourseOffering(courseOffering);
		return Response.ok().build();
	}

	@DELETE
	@Path("/courseofferings")
	public Response deleteCourseOffering(String courseOfferingCode) {
		courseOfferingMgtService.deleteCourseOffering(courseOfferingCode);
		return Response.ok().build();
	}

	@GET
	@Path("/courseofferings")
	public List<CourseOffering> getCourseOfferings() {
		return courseOfferingMgtService.getCourseOfferings();
	}

	@GET
	@Path("/courseofferings/{courseOfferingCode}")
	public CourseOffering getCourseOffering(
			@PathParam(value = "courseOfferingCode") String courseOfferingCode) {
		return courseOfferingMgtService.getCourseOffering(courseOfferingCode);
	}

	@GET
	@Path("/courseofferings/byauthor")
	public List<CourseOffering> getCourseOfferingsByAuthor(
			@QueryParam(value = "authorId") String authorId) {
		return courseOfferingMgtService.getCourseOfferingsByAuthor(authorId);
	}

	@GET
	@Path("/courseofferings/{courseOfferingCode}/enrolled")
	public List<CourseOffering> getEnrolledCourseOfferings(
			@QueryParam(value = "userId") String userId) {
		return courseOfferingMgtService.getEnrolledCourseOfferings(userId);
	}

	@GET
	@Path("/courseofferings/{courseOfferingCode}/authored")
	public List<CourseOffering> getAuthoredCourseOfferings() {
		return courseOfferingMgtService.getAuthoredCourseOfferings();
	}

	public List<CourseOffering> addAuthors(String courseOfferingCode,
			List<String> authorIds) {
		// TODO Auto-generated method stub
		return null;
	}

	@POST
	@Path("/courseofferings/{courseOfferingCode}/lessons")
	public Response addLesson(Lesson lesson,
			@PathParam(value = "courseOfferingCode") String courseOfferingCode) {
		Lesson l = courseOfferingMgtService.addLesson(lesson,
				courseOfferingCode);
		return Response.ok(l).build();
	}

	@PUT
	@Path("/courseofferings/{courseOfferingCode}/lessons")
	public Response updateLesson(Lesson lesson,
			@PathParam(value = "courseOfferingCode") String courseOfferingCode) {
		courseOfferingMgtService.updateLesson(lesson, courseOfferingCode);
		return Response.ok().build();
	}

	@DELETE
	@Path("/courseofferings/{courseOfferingCode}/lessons/{lessonName}")
	public Response deleteLesson(
			@PathParam(value = "courseOfferingCode") String courseOfferingCode,
			@PathParam(value = "lessonName") String lessonName) {
		courseOfferingMgtService.deleteLesson(courseOfferingCode, lessonName);
		return Response.ok().build();
	}

	@GET
	@Path("/courseofferings/{courseOfferingCode}/lessons")
	public List<Lesson> getLessons(
			@PathParam(value = "courseOfferingCode") String courseOfferingCode) {
		return courseOfferingMgtService.getLessons(courseOfferingCode);
	}

	@GET
	@Path("/courseofferings/{courseOfferingCode}/lessons/{lessonName}")
	public Lesson getLesson(
			@PathParam(value = "courseOfferingCode") String courseOfferingCode,
			String lessonName) {
		return courseOfferingMgtService.getLesson(courseOfferingCode,
				lessonName);
	}

	@POST
	@Path("/courseofferings/{courseOfferingCode}/lessons/{lessonName}/units")
	public Response addLessonUnit(LessonUnit lessonUnit,
			@PathParam(value = "courseOfferingCode") String courseOfferingCode,
			@PathParam(value = "lessonName") String lessonName) {
		LessonUnit lu = courseOfferingMgtService.addLessonUnit(lessonUnit,
				courseOfferingCode, lessonName);
		return Response.ok(lu).build();
	}

	@PUT
	@Path("/courseofferings/{courseOfferingCode}/lessons/{lessonName}/units")
	public Response updateLessonUnit(LessonUnit lessonUnit,
			@PathParam(value = "courseOfferingCode") String courseOfferingCode,
			@PathParam(value = "lessonName") String lessonName) {
		courseOfferingMgtService.updateLessonUnit(lessonUnit,
				courseOfferingCode, lessonName);
		return Response.ok().build();
	}

	@DELETE
	@Path("/courseofferings/{courseOfferingCode}/lessons/{lessonName}/units/{lessonUnitName}")
	public Response deleteLessonUnit(
			@PathParam(value = "courseOfferingCode") String courseOfferingCode,
			@PathParam(value = "lessonName") String lessonName,
			@PathParam(value = "lessonUnitName") String lessonUnitName) {
		courseOfferingMgtService.deleteLessonUnit(courseOfferingCode,
				lessonName, lessonUnitName);
		return Response.ok().build();
	}

	@GET
	@Path("/courseofferings/{courseOfferingCode}/lessons/{lessonName}/units/{lessonUnitName}")
	public LessonUnit getLessonUnit(
			@PathParam(value = "courseOfferingCode") String courseOfferingCode,
			@PathParam(value = "lessonName") String lessonName,
			@PathParam(value = "lessonUnitName") String lessonUnitName) {
		return courseOfferingMgtService.getLessonUnit(courseOfferingCode,
				lessonName, lessonUnitName);
	}

	@GET
	@Path("/courseofferings/{courseOfferingCode}/lessons/{lessonName}/units")
	public List<LessonUnit> getLessonUnits(
			@PathParam(value = "courseOfferingCode") String courseOfferingCode,
			@PathParam(value = "lessonName") String lessonName) {
		return courseOfferingMgtService.getLessonUnits(courseOfferingCode,
				lessonName);
	}

//	@POST
//	@Path("/courseofferings/resources")
//	@Consumes(MediaType.MULTIPART_FORM_DATA)
//	@WebMethod(operationName = "uploadResource")
//	// public void addContent(InputStream contentAsStream, String
//	// contentCtxType,
//	// String contentContextId, String contentType, String contentId,
//	// Map<String, String> properties, Map<String, String> parentContextMap)
//	public Response uploadResource(
//			List<Attachment> attachments,
//			@Context HttpServletRequest request,
//			@QueryParam(value = "courseOfferingCode") String courseOfferingCode,
//			@QueryParam(value = "lessonCode") String lessonCode,
//			@QueryParam(value = "lessonUnitCode") String lessonUnitCode,
//			@QueryParam(value = "contentCtxType") String contentCtxType) {
//		for (Attachment attachment : attachments) {
//			DataHandler handler = attachment.getDataHandler();
//			try {
//				InputStream stream = handler.getInputStream();
//				MultivaluedMap<String, String> map = attachment.getHeaders();
//				courseOfferingMgtService.addContent(stream,
//						contentCtxType, contentContextId, contentType,
//						contentId, properties, parentContextMap);
//
//			} catch (Exception e) {
//				return Response.serverError().build();
//			}
//		}
//	}

}
